<template>
  <a href="javascript:;"
    :class="btnClass"
    :title="text"
    @mouseover="onMouse(true)"
    @mouseleave="onMouse(false)"
    @click.stop.prevent="bindBtnClick($event)">
    <img :src="imgSrc"/>
    <span>{{text}}</span>
  </a>
</template>

<script>
export default {
  name: 'icon-button',
  props: {
    text: {
      type: String,
      required: false,
      default: () => ('')
    },
    icon_normal: {
      type: String,
      required: false,
      default: null
    },
    icon_hot: {
      type: String,
      required: false,
      default: null
    },
    params: {
      type: Object,
      required: false,
      default: null
    },
    enabled: {
      type: Boolean,
      required: false,
      default: true
    },
    selected: {
      type: Boolean,
      required: false,
      default: false
    },
    lineClass: {
      type: Object,
      required: false,
      default: () => ({ 'icon-button': true })
    },
    onClick: {
      type: Function,
      required: false
    }
  },
  data () {
    return {
      over: false,
      imgSrc: null
    }
  },
  mounted () {
    this.resetImage()
  },
  updated () {
    this.resetImage()
  },
  methods: {
    bindBtnClick (e) {
      this.enabled && this.onClick && this.onClick(this.params, e)
    },
    onMouse (over) {
      this.over = over
      this.resetImage()
    },
    resetImage () {
      this.imgSrc = (this.selected || this.over) && this.enabled ? this.icon_hot : this.icon_normal
    }
  },
  computed: {
    btnClass () {
      return Object.assign({}, this.lineClass, { 'is-disabled': !this.enabled, 'selected': this.selected })
    }
  }
}
</script>

<style lang="scss">
@import "~@/css/common/variables";
@import "~@/css/lib/mixin";

.icon-button {
  height: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: #282b30;
  font-weight: bold;
  font-size: 10pt;
  border-radius: 5px;
  border: none;
  span {
    width: 100%;
    height: 15px;
    text-align: center;
  }
  &::before {
    content: '';
    display: table;
    margin-left: 20px;
    width: 6px;
    height: 6px;
    background: #ff6600;
    border-radius: 50%;
  }
  &:hover {
    cursor: pointer;
    color: #ff6600;
    border: 1px solid $bg-color-hover;
  }
  &.selected {
    color: #ff6600;
  }
  &.is-disabled {
    background-color: #9a9aa9;
    cursor: not-allowed;
  }

}
</style>
